<?php

/**
 * frecuencias actions.
 *
 * @package    SistemaRutasEc
 * @subpackage frecuencias
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class frecuenciasActions extends sfActions {

    public function executeVisita(sfWebRequest $request) {

        $tiru = $request->getParameter('tiporuta');
        if ($tiru == 0) {
            $this->tipo = "Cantonales";
        } else if ($tiru == 1) {
            $this->tipo = "Provinciales";
        } else if ($tiru == 2) {
            $this->tipo = "Internacionales";
        }


        $this->cooperativas = Doctrine_Core::getTable('cooperativa')
                ->createQuery('c')
                ->execute();

        $this->rutas = Doctrine::getTable('ruta')->createQuery('r')
                ->where('r.tipoRuta = ?', $tiru)
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru')));
        $this->forward404Unless($this->ruta);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new rutaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new rutaForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $this->form = new rutaForm($ruta);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $this->form = new rutaForm($ruta);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $ruta->delete();

        $this->redirect('frecuencias/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $ruta = $form->save();

            $this->redirect('frecuencias/edit?id_ru=' . $ruta->getIdRu());
        }
    }

}
